package sailing.ui.tabbedPane;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.KeyboardFocusManager;
import java.awt.Rectangle;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;

import com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI;

import cjlite.logger.Logger;

/**
 * Windows rendition of the component.
 * <p>
 * <strong>Warning:</strong> Serialized objects of this class will not be compatible with future Swing releases. The
 * current serialization support is appropriate for short term storage or RMI between applications running the same
 * version of Swing. A future release of Swing will provide support for long term persistence.
 */
public class MyWindowsTabbedPaneUI extends WindowsTabbedPaneUI {

	private static Logger logger = Logger.thisClass();
	/**
	 * Keys to use for forward focus traversal when the JComponent is managing focus.
	 */
	private static Set<KeyStroke> managingFocusForwardTraversalKeys;

	/**
	 * Keys to use for backward focus traversal when the JComponent is managing focus.
	 */
	private static Set<KeyStroke> managingFocusBackwardTraversalKeys;

	private Color selectedColor;
	protected Insets tabInsets;
	private int tabGap = 3;

	private boolean contentOpaque = true;

	protected void installDefaults() {
		super.installDefaults();
		contentOpaque = UIManager.getBoolean("TabbedPane.contentOpaque");

		selectedColor = new Color(204, 230, 255);

		// focus forward traversal key
		if (managingFocusForwardTraversalKeys == null) {
			managingFocusForwardTraversalKeys = new HashSet<KeyStroke>();
			managingFocusForwardTraversalKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
		}
		tabPane.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, managingFocusForwardTraversalKeys);
		// focus backward traversal key
		if (managingFocusBackwardTraversalKeys == null) {
			managingFocusBackwardTraversalKeys = new HashSet<KeyStroke>();
			managingFocusBackwardTraversalKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));
		}
		tabPane.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, managingFocusBackwardTraversalKeys);
	}

	protected void uninstallDefaults() {
		// sets the focus forward and backward traversal keys to null
		// to restore the defaults
		tabPane.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);
		tabPane.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);
		super.uninstallDefaults();
	}

	public static ComponentUI createUI(JComponent c) {
		return new MyWindowsTabbedPaneUI();
	}

	@Override
	protected int calculateMaxTabHeight(int tabPlacement) {
		return 28;
	}

}
